home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.05 May 91 / Fractal Code / MountainsToolbox.c < prev   
Encoding:
C/C++ Source or Header  |  1990-01-14  |  4.4 KB  |  177 lines  |  [TEXT/KAHL]

  1. /*
  2.     MountainsToolbox.c
  3.     The toolbox interface to front-end MountainsAlgorithm.c
  4.     By Ben Haller 1/1/90
  5.     ©1990 Ben Haller
  6. */
  7.  
  8. #include "Mountains.h"
  9.  
  10. char done=FALSE;                /* FALSE as long as user has not quit    */
  11. MenuHandle menu[6];                /* Our menus                            */
  12. EventRecord evt;                /* Our eventrecord                        */
  13. WindowPtr wp;                    /* Our window                            */
  14. CursHandle watch;                /* Watch cursor for long waits            */
  15. char colorF;                    /* TRUE if 8-bit color                    */
  16. int wx,wy,wd;                    /* Size of window (x and y dimensions)    */
  17. Rect r;                            /* scratch rectangle for general use    */
  18. char menuCk[5]={3,6,5,5,1};        /* number of checked item in each menu    */
  19.                                 /*    (File menu not included)            */
  20.  
  21. /********************************************************/
  22. /* Routines in MountainsAlgorithm.c                        */
  23. void DrawMountains(void);
  24. void CalcMountains(void);
  25.  
  26. /********************************************************/
  27. /* Initializes the toolbox and all important variables,    */
  28. /* loads resources, etc.                                */
  29. void Init()
  30. {
  31.     int n;
  32.     
  33.     InitGraf(&thePort);
  34.     InitFonts();
  35.     InitWindows();
  36.     InitMenus();
  37.     TEInit();
  38.     InitDialogs(0L);
  39.     InitCursor();
  40.     FlushEvents(-1, 0);
  41.     
  42.     /* Reseed the random number generator.  Note: You can force generation        */
  43.     /*    of a specific series of random numbers (and thus a specific picture,    */
  44.     /*    all parameters being equal) by replacing this statement with            */
  45.     /*    "randSeed=<any integer>"                                                */
  46.     randSeed=Ticks;
  47.         
  48.     /* Find out if we're running in 8-bit color and set colorF accordingly        */
  49.     n=(*((*GetGDevice())->gdPMap))->pixelSize;
  50.     colorF=(n>=8);
  51.     
  52.     /* Make menus                                    */
  53.     ClearMenuBar();
  54.     menu[0]=NewMenu(5000,"\pFile");
  55.     AppendMenu(menu[0],"\pCalculate;(-;Quit");
  56.     InsertMenu(menu[0],0);
  57.     menu[1]=NewMenu(5001,"\pIterations");
  58.     AppendMenu(menu[1],"\p3;4;5;6;7;8;9");
  59.     InsertMenu(menu[1],0);
  60.     menu[2]=NewMenu(5002,"\pContour");
  61.     AppendMenu(menu[2],"\p0.25;0.50;0.75;1.00;1.25;1.50;1.75;2.00;3.00;5.00");
  62.     InsertMenu(menu[2],0);
  63.     menu[3]=NewMenu(5003,"\pSmoothness");
  64.     AppendMenu(menu[3],"\p1.00;1.25;1.50;1.75;2.00;2.25;2.75;3.50;5.00;∞");
  65.     InsertMenu(menu[3],0);
  66.     menu[4]=NewMenu(5004,"\pHeight");
  67.     AppendMenu(menu[4],"\p1;2;3;4;5;6;7;8;9;10");
  68.     InsertMenu(menu[4],0);
  69.     menu[5]=NewMenu(5005,"\pProfile");
  70.     AppendMenu(menu[5],"\p#1;#2;#3;#4");
  71.     InsertMenu(menu[5],0);
  72.     DrawMenuBar();
  73.     
  74.     /* Check all the menu items appropriate            */
  75.     for (n=1; n<6; n++)
  76.         CheckItem(menu[n],menuCk[n-1],TRUE);
  77.     
  78.     /* Get watch                                    */
  79.     watch=GetResource('CURS',4);
  80.     HLock(watch);
  81.     
  82.     /* Make our window, full screen inset 5 pixels    */
  83.     r=screenBits.bounds;
  84.     r.top+=MBarHeight;
  85.     InsetRect(&r,5,5);
  86.     wx=r.right-r.left;
  87.     wy=r.bottom-r.top;
  88.     if (wy>wx/2) wy=wx/2;
  89.     else if (wx>wy*2) wx=wy*2;
  90.     wd=wx;
  91.     r.bottom=r.top+wy;
  92.     r.right=r.left+wx;
  93.     if (colorF)
  94.         wp=NewCWindow(0L, &r, "\p", TRUE, plainDBox, -1L, FALSE, 0L);
  95.     else
  96.         wp=NewWindow(0L, &r, "\p", TRUE, plainDBox, -1L, FALSE, 0L);
  97. }
  98.  
  99. /********************************************************/
  100. /* Handle the selection of a menu item                    */
  101. void HandleMenu(m,i)
  102. int m,i;            /* m=menu id, i=item number            */
  103. {
  104.     m-=5000;
  105.     switch (m) {
  106.         case 0:
  107.             if (i==1) {                    /* Recalculate and update    */
  108.                 CalcMountains();
  109.                 InvalRect(&(wp->portRect));
  110.             } else done=TRUE;            /* Quit                        */
  111.             break;
  112.         case 1:
  113.         case 2:
  114.         case 3:
  115.         case 4:
  116.         case 5:                            /* Move the check            */
  117.             CheckItem(menu[m],menuCk[m-1],FALSE);
  118.             menuCk[m-1]=i;
  119.             CheckItem(menu[m],menuCk[m-1],TRUE);
  120.             break;
  121.     }
  122.     if (!done) HiliteMenu(0);
  123. }
  124.  
  125. /********************************************************/
  126. /* Handle updating our window                            */
  127. void HandleUpdate(wp)
  128. WindowPtr wp;
  129. {
  130.     SetPort(wp);
  131.     BeginUpdate(wp);
  132.     if (colorF) {
  133.         RGBColor blackc;
  134.         
  135.         blackc.red=0; blackc.green=0; blackc.blue=0;
  136.         RGBForeColor(&blackc);
  137.     }
  138.     FillRect(&(wp->portRect),black);
  139.     DrawMountains();
  140.     EndUpdate(wp);
  141. }
  142.  
  143. /********************************************************/
  144. /* Handle a mouse click                                    */
  145. void HandleMouse()
  146. {
  147.     WindowPtr wW;
  148.     long msresult;
  149.     
  150.     switch (FindWindow(evt.where, &wW)) {
  151.         case inMenuBar:
  152.             msresult=MenuSelect(evt.where);
  153.             HandleMenu(HiWord(msresult),LoWord(msresult));
  154.             break;
  155.         default: break;
  156.     }
  157. }
  158.  
  159. /********************************************************/
  160. /* Initialize, get events until done.                    */
  161. int main()
  162. {
  163.     Init();
  164.     while (!done) {
  165.         SystemTask();
  166.         if (GetNextEvent(everyEvent, &evt)) {
  167.             switch (evt.what) {
  168.                 case mouseDown:
  169.                     HandleMouse();
  170.                     break;
  171.                 case updateEvt:
  172.                     HandleUpdate(evt.message);
  173.                     break;
  174.             }
  175.         }
  176.     }
  177. }